package com.sunday.android.fangdaifu.bukong.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sunday.android.fangdaifu.bukong.R;
import com.sunday.android.fangdaifu.bukong.activity.WebViewActivity;
import com.sunday.android.fangdaifu.bukong.entity.HouseDetail;
import com.sunday.android.fangdaifu.bukong.entity.HouseItem;
import com.sunday.android.fangdaifu.bukong.http.ApiClient;
import com.sunday.android.fangdaifu.bukong.utils.Constants;
import com.sunday.common.widgets.NoScrollListview;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 刘涛 on 2017/11/7.
 */

public class HouseDetailListAdapter extends RecyclerView.Adapter {


    private Context mContext;
    private List<HouseDetail> dataSet;

    public HouseDetailListAdapter(Context context, List<HouseDetail> datas) {
        this.mContext = context;
        this.dataSet = datas;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.list_house_detail_item, null);
        return new ListHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        HouseDetail item = dataSet.get(position);
        final ListHolder listHolder = (ListHolder) holder;
        if (!item.getChildren().isEmpty()) {
            listHolder.adapter.setDatas(item.getChildren());
            listHolder.adapter.notifyDataSetChanged();
            listHolder.listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent(mContext, WebViewActivity.class);
                    HouseItem houseItem= (HouseItem) listHolder.adapter.getItem(position);
                    intent.putExtra(Constants.TITLE,"设备详情");
                    String url= ApiClient.API_URL+"/mobi/dispatched/waring/history?plantId="+houseItem.getPlantId();
                    intent.putExtra(Constants.URL,url);
                    mContext.startActivity(intent);
                }
            });
        }
        listHolder.title.setText(item.getType());
        listHolder.title.setVisibility(dataSet.size()>1?View.VISIBLE:View.GONE);

    }

    @Override
    public int getItemCount() {
        return dataSet.size();
    }

    class ListHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.title)
        TextView title;
        @Bind(R.id.list_view)
        NoScrollListview listView;
        @Bind(R.id.ll_house_equipment)
        LinearLayout llHouseEquipment;

        private HouseEquipmentAdapter adapter;

        public ListHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            adapter = new HouseEquipmentAdapter(mContext);
            listView.setAdapter(adapter);
        }
    }


}
